#include<iostream>
using namespace std;

class BinaryHeap
{
public:
    int* H;
    const int capacity;
    int size;
    int dead;   // first dead ele pos
    BinaryHeap(int _capa):capacity{_capa}
    {
        H = new int[capacity];
        dead = size = capacity; 
        for(int i = 0;i<capacity;++i) scanf("%d",H+i); 
        for(int i = size/2 - 1; i>=0;--i) PercoDown(i); 
    }
    ~BinaryHeap(){delete[] H;}
    inline int& operator[](int i) { return H[i]; }
    void PercoDown(int i);
};

void BinaryHeap::PercoDown(int i)
{
    int tmp = H[i];
    while(2*i+1 < size)
    {
        int smaller = (2*i+2>=size || H[2*i+1]<H[2*i+2]) ? 2*i+1 : 2*i+2;
        if(H[smaller] < tmp)
        {
            H[i] = H[smaller];
            i = smaller;
        }
        else break;
    }
    H[i] = tmp;
}


int main(int argc, char const *argv[])
{
    int N,M; scanf("%d%d",&N,&M);
    BinaryHeap H(M);
    int cnt = N-M;
    int flag = 1;
    while(H.size || cnt || H.dead!=H.capacity)
    {
        if(H.size) 
        {
            if(flag) flag = 0;
            else printf(" ");
            printf("%d",H[0]);
        }
        else
        {
            printf("\n"); flag = 1;
            int dead_num = H.capacity-H.dead;
            for(int i = 0;i<dead_num;++i)  H[i] = H[H.dead++]; 
            H.size = dead_num;
            for(int i = H.size/2 - 1; i>=0;--i) H.PercoDown(i); 
            continue;
        }
        if(cnt)
        {
            int num; scanf("%d",&num); cnt--;
            if(num<H[0]) { H[0] = H[--H.size]; H[--H.dead] = num; }
            else  H[0] = num; 
        }
        else  H[0] = H[--H.size]; 
        H.PercoDown(0);
    }
    return 0;
}